'use client';

import { useSingleton } from 'foxact/use-singleton';
import '@amap/amap-jsapi-types';

export default function AmapProvider({
  children,
  amapKey,
  secretKey,
}: {
  children?: React.ReactNode;
  amapKey: string;
  secretKey: string;
}) {
  useSingleton(async () => {
    if (typeof window !== 'undefined') {
      const AMapLoader = await import('@amap/amap-jsapi-loader');
      (window as any)._AMapSecurityConfig = {
        securityJsCode: secretKey,
      };
      AMapLoader.load({
        key: amapKey,
        version: '2.0',
        plugins: ['AMap.PlaceSearch'],
      });
      return {};
    }
  });

  return children;
}
